package com.zhiyou.app.shop.manager.dao.impl;

import org.hibernate.Query;
import org.springframework.stereotype.Component;

import com.zhiyou.app.shop.common.ShopManageConstants;
import com.zhiyou.app.shop.manager.dao.IGoodsCategoryDao;
import com.zhiyou.app.shop.manager.dao.pojo.Goods;
import com.zhiyou.app.shop.manager.dao.pojo.GoodsCategory;

@Component
public class GoodsCategoryDaoImpl extends BaseHibernateDAO<GoodsCategory, String> implements IGoodsCategoryDao {
	
	public GoodsCategoryDaoImpl(){
		super(GoodsCategory.class);
	}

	@Override
	public void save(GoodsCategory cat) {
		cat.setId(this.generateCategoryId());
		super.save(cat);
	}
	
	private String generateCategoryId(){
		String hql = "select max(id) from GoodsCategory";
		Query query = this.getSession().createQuery(hql);
		Object obj = query.uniqueResult();
		String maxId = ShopManageConstants.rootCategoryId;
		if(null != obj){
			maxId = obj.toString();
		}
		String beginStr = maxId.substring(0, 4);
		Long newBegin = Long.valueOf(beginStr) + 1;
		return newBegin.toString()+maxId.substring(beginStr.length());
	}
	
	public int getMaxOrderNumber() {
		String hql = "select max(orderNumber) from GoodsCategory";
		Query query = this.getSession().createQuery(hql);
		Object obj = query.uniqueResult();
		if(null != obj){
			return Integer.valueOf(obj.toString());
		}
		return 0;
	}

}
